import json
import random
from pathlib import Path
from dataclasses import dataclass
from typing import NamedTuple


class PunchProfile(NamedTuple):
    choices: int
    threshold: int
    hit_damage: int
    blocked_damage: int

    pre_msg: str
    hit_msg: str
    blocked_msg: str

    knockout_possible: bool = False

    def is_hit(self):
        return random.randint(1, self.choices) <= self.threshold


@dataclass
class Player:
    name: str
    best: str
    weakness: int
    # 可以通过is_computer来判断目前是玩家还是电脑
    is_computer: bool
    # 你可以用Literal来限制input
    punch_profiles: Dict[Literal[1, 2, 3, 4], PunchProfile]
    damage: int = 0
    score: int = 0

    def get_punch_choise(self) -> Literal[1, 2, 3, 4]:
        # 生成电脑的拳法通过随机数
        if self.is_computer:
            return random.randint(1, 4)
        punch = -1
        # 通过while死循环要求玩家必须选择1~4中任意一个拳法
        while punch not in [1, 2, 3, 4]:
            printf(f"{self.name}的招式是？")
            # 可以加上是不是数字的判断以免不是数字卡死报错
            punch = int(input())
        return punch


# 击倒阈值
KNOCKOUT_THRESHOLD = 35
# 击晕信息
KNOCKOUT_COLD = "{loser}被击晕了无法继续进行比赛，胜利者属于{winner}"


# 获取选手的弱点信息
def get_vulnerability():
    printf("他的弱点是？")
    # 对于数字的判断
    return int(input())


# 获取电脑的弱点信息
def get_opponent_stats() -> Tuple[int, int]:
    opponent_best = 0
    opponent_weakness = 0
    while opponent_best == opponent_weakness:
        opponent_best = random.randint(1, 4)
        opponent_weakness = random.randint(1, 4)

    return opponent_best, opponent_weakness


# 获取招式的基本信息
def read_punch_profiles(filepath: Path):
    with open(filepath, encoding='utf-8') as f:
        punch_profiles_dict = json.load(f)
    return {
        cast(Literal[1, 2, 3, 4], int(key)): PunchProfile(**value)
        for key, value in punch_profiles_dict.items()
    }


def main() -> None:
    print("拳击游戏")
    print("design by xmc")
    print("由xmc设计创造")
    print("学号：202312060332")

    print("你对手的名字是？")
    opponent_name = input()

    print("你的名字是？")
    player_name = input()

    print("不同的出拳策略 1摆拳 2平勾拳 3上勾拳 4刺拳")
    print("你擅长的招式是？")
    player_best = int(input)
    player_weakness = get_vulnerability()
    player = Player(
        name=player_name,
        best=player_best,
        weakness=player_weakness,
        is_computer=False,
        punch_profiles=read_punch_profiles(
            Path(__file__).parent / "player-profile.json"
        ),
    )

    opponent_best, opponent_weakness = get_opponent_stats()
    opponent = Player(
        name=opponent_name,
        best=opponent_best,
        weakness=opponent_weakness,
        is_computer=True,
        punch_profiles=read_punch_profiles(
            Path(__file__).parent / "opponent-profile.json"
        ),
    )
    # 做一次判断，看看能不能读取对应的文件数据
    print(player)
    print(opponent)


if __name__ == "__main__":